using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using static UnityEditor.PlayerSettings;
public class CardItem : MonoBehaviour, IBeginDragHandler, IEndDragHandler, IDragHandler
{
    public DragCardItem dragCard;

    public Text nameTxt;

    public void OnBeginDrag(PointerEventData eventData)
    {
        dragCard.gameObject.SetActive(true);
        dragCard.nameTxt.text = nameTxt.text;
    }

    public void OnDrag(PointerEventData eventData)
    {
        Vector2 pos;
        if (RectTransformUtility.ScreenPointToLocalPointInRectangle(dragCard.parentTf, eventData.position, eventData.pressEventCamera, out pos))
        {
            dragCard.rectTf.anchoredPosition = pos;
        }
    }

    public void OnEndDrag(PointerEventData eventData)
    {
        dragCard.gameObject.SetActive(false);



        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

        RaycastHit hit;

        if (Physics.Raycast(ray, out hit, Mathf.Infinity, LayerMask.GetMask("Ground")))
        {
            GameObject obj = SimpleFactory.CreateObject("Model/" + dragCard.nameTxt.text);

            Vector3 pos = hit.point;

            pos.x = hit.transform.position.x;

            pos.y = -1;

            obj.transform.position = pos;

            ModelView view = obj.AddComponent<ModelView>();
            view.Round = int.Parse(hit.transform.name);
            GameManager.Instance.AddPlayer(view);
            GameManager.Instance.SortPlayer(view);

            if (GameManager.Instance.gameState == GameState.Fight)
            {
                view.BeginMove();
            }
        }
    }

    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {

    }
}
